﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ThirdServices.TemperatureSettings;
using ThirdServices.AlertModel.Analyzers;
using ThirdServices.AlertModel.Factory.Entities.DTO;
using ThirdServices.AlertModel.Factory.BaseInterface;

namespace ThirdServices.AlertModel.Providers.Norway
{
    public class NorwayTempAlert : IAlert
    {
        private double lat;
        private double lon;
        private double radius;
        private double percentage;
        private int periodInDays;
        private int numDataPerDay;

        public double Percentage
        {
            get { return percentage; }
            set { percentage = value; }
        }

        public int PeriodInDays
        {
            get { return periodInDays; }
            set { periodInDays = value; }
        }

        public double Lat
        {
            get { return lat; }
            set { lat = value; }
        }

        public double Lon
        {
            get { return lon; }
            set { lon = value; }
        }

        public int NumDataPerDay
        {
            get { return numDataPerDay; }
            set { numDataPerDay = value; }
        }

        #region IAlert Members

        public List<BaseAlert> CheckAlert()
        {
            List<TemperatureData> TempData = new List<TemperatureData>();
            Console.WriteLine("NorwayTemp : recieving information...");
            string url = AlertsLinks.NorwayWindAlertLink(lat, lon);
            XDocument xDoc = XDocument.Load(url);
            foreach (XElement xe in xDoc.Descendants("time"))
            {
                try
                {
                    TempData.Add(new TemperatureData
                                     {
                                         Date = Convert.ToDateTime(xe.Attribute("from").Value, CultureInfo.InvariantCulture),
                                         Temperature = Convert.ToDouble(xe.Element("location").Element("temperature").Attribute("value").Value.ToString(), CultureInfo.InvariantCulture)
                                     });
                }
                catch (Exception ex) { break; }
            }
            List<BaseAlert> baseAlerts = TempAnalyser.AnalyseTemperatureData(TempData, percentage, periodInDays,numDataPerDay);

            return baseAlerts;

        }

        #endregion

        public void GetAlert()
        {
        }
    }
}